<template>
  <!-- 添加或修改车辆备案对话框 -->
  <el-dialog :title="title" :visible.sync="dialogVisible" width="1000px" append-to-body :close-on-click-modal="false"
    @close="close">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px" label-position="right" class="flex-form"
      size="small">

      <el-form-item label="境内车牌号" prop="domesticVehicleNo">
        <el-input v-model.trim="form.domesticVehicleNo" placeholder="请输入境内车牌号" />
      </el-form-item>

      <el-form-item label="境外车牌号" prop="abroadVehicleNo">
        <el-input v-model.trim="form.abroadVehicleNo" placeholder="请输入境外车牌号" />
      </el-form-item>

      <el-form-item label="车辆自重" prop="selfWt">
        <el-input-number v-model="form.selfWt"  :min="0" :max="999999999" placeholder="请输入车辆自重" />
      </el-form-item>
      <el-form-item label="车辆类型" prop="vehicleType">
        <el-select v-model.trim="form.vehicleType" disabled placeholder="请选择车辆类型">
          <el-option v-for="dict in vehicleTypeList"  :key="dict.value" :label="dict.label"
            :value="dict.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="有效期" prop="expirationDate">
        <el-date-picker clearable size="small" v-model.trim="form.expirationDate" type="date"
          value-format="yyyy-MM-dd HH:mm:ss" placeholder="选择有效期">
        </el-date-picker>
      </el-form-item>

      <el-form-item label="主管海关" prop="customsCode">
        <el-input v-model.trim="form.customsCode" placeholder="请输入主管海关" />
      </el-form-item>

      <el-form-item label="外省备案" prop="otherProvincesReg">
        <el-select v-model.trim="form.otherProvincesReg" @change="otherProvincesRegValChange" placeholder="请选择是否外省备案">
          <el-option
              v-for="dict in dict.type.yes_or_no"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="车辆海关编号" prop="veCustomsNo">
        <el-input v-model.trim="form.veCustomsNo" :disabled="form.otherProvincesReg=='0'" placeholder="请输入车辆海关编号" />
      </el-form-item>

     <!-- <el-form-item label="IC卡号" prop="cardNo">
        <el-input v-model.trim="form.cardNo" placeholder="请输入IC卡号" />
      </el-form-item>

      <el-form-item label="电子车牌" prop="rfid">
        <el-input v-model.trim="form.rfid" placeholder="请输入电子车牌" />
      </el-form-item> -->
      <el-form-item label="车架号" prop="frameNo">
        <el-input v-model.trim="form.frameNo" placeholder="请输入车架号" />
      </el-form-item>
      <el-form-item label="司机姓名" prop="driverName">
        <el-input v-model.trim="form.driverName" placeholder="请输入司机姓名" />
      </el-form-item>
      <el-form-item label="车辆结构" prop="regType">
        <el-select v-model.trim="form.regType" placeholder="请选择车辆结构">
         <el-option v-for="dict in dict.type.fee_config_vehicle_type" :key="dict.value" :label="dict.label"
           :value="dict.value"></el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="close">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
  import {
    getPreVehicleRecord,
    addPreVehicleRecord,
    updatePreVehicleRecord
  } from "@/api/billsdecl/preVehicleRecord";
  export default {
    name: "PreVehicleRecord-addEdit",
    props: {
      dialogType: {
        type: String,
        default: "add",
      },
      rowData: {
        type: Object,
        default: () => {},
      },
      dict: {}
    },
    data() {
      return {
        // 弹出层
        dialogVisible: false,
        // 标题
        title: "新增",
        vehicleTypeList: [
          {label: "跨境车", value: "1"},
          {label: "提送货车", value: "2"},
         // {label: "行政车辆", value: "3"},
        ],
        // 表单参数
        form: {
          id: null,
          veCustomsNo: null,
          customsCode: null,
          fieldCode: null,
          fieldName: null,
          frameNo: null,
          domesticVehicleNo: null,
          abroadVehicleNo: null,
          selfWt: null,
          vehicleType: '2',
          regType: null,
          cardNo: null,
          rfid: null,
          otherProvincesReg:'0',
          driverName: null,
          expirationDate: null,
          createBy: null,
          createTime: null,
          updateBy: null,
          updateTime: null,
          declStatus: null,
          useStatus: null,
          declTime: null,
          declBy: null,
          declRetryNum: null,
          auditTime: null,
          auditRemark: null

        },
        // 车辆备案表格数据
        preVehicleRecordList: [],
        // 表单校验
        rules: {
          customsCode: [{
            required: true,
            message: "主管海关不能为空",
            trigger: "blur"
          }],
          fieldCode: [{
            required: true,
            message: "场所代码不能为空",
            trigger: "blur"
          }],
          otherProvincesReg: [{
            required: true,
            message: "是否外省运管备案不能为空",
            trigger: "blur"
          }],
          fieldName: [{
            required: true,
            message: "场所名称不能为空",
            trigger: "blur"
          }],
          vehicleType: [{
            required: true,
            message: "车辆结构不能为空",
            trigger: "change"
          }],
          veCustomsNo: [{
            required: false,
            message: "车辆海关编码不能为空",
            trigger: "change"
          }],

          expirationDate: [{
            required: true,
            message: "批文有效期不能为空",
            trigger: "blur"
          }],
          createBy: [{
            required: true,
            message: "创建人不能为空",
            trigger: "blur"
          }],
          createTime: [{
            required: true,
            message: "创建时间不能为空",
            trigger: "blur"
          }],
          declStatus: [{
            required: true,
            message: "申报状态不能为空",
            trigger: "change"
          }],
          useStatus: [{
            required: true,
            message: "使用状态不能为空",
            trigger: "change"
          }],
        }
      };
    },
    methods: {
      open() {
        this.dialogVisible = true;
        this.$nextTick(() => {
          if (this.dialogType == "add") {
            this.title = "新增";
          } else if (this.dialogType == "edit") {
            this.title = "编辑";
            this.loadDetail();
          } else if (this.dialogType == "detail") {
            this.title = "详情";
            this.loadDetail();
          }
        });
      },
      close() {
        this.$refs["form"].resetFields();
        this.dialogVisible = false;
      },

      // 获取详情
      loadDetail() {
        const id = this.rowData.id;
        getPreVehicleRecord(id).then((res) => {
          if (res.code == 200) {
            this.form = res.data;
          }
        });
      },

      otherProvincesRegValChange(val){
        if('1'==val){
          this.rules.veCustomsNo[0].required=true;
          this.form.vehicleType='1';
        }else{
             this.rules.veCustomsNo[0].required=false;
          if(this.dialogType == "add"){
            this.form.veCustomsNo='';
          }
        }
      },

      // 提交表单
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            let params = {
              ...this.form,
            };
            if (this.dialogType == "add") {
              addPreVehicleRecord(this.form).then(res => {
                if (res.code === 200) {
                  this.$modal.msgSuccess("新增成功");
                  this.close();
                  this.$emit("handleQuery");
                }
              });
            } else {
              params.id = this.rowData.id;
              updatePreVehicleRecord(this.form).then(res => {
                if (res.code === 200) {
                  this.$modal.msgSuccess("修改成功");
                  this.close();
                  this.$emit("handleQuery");
                }
              });
            }
          }
        });
      },
    },
  };
</script>

<style lang="scss" scoped></style>
